<?php


class EmsPortalTable extends Doctrine_Table
{
    static public $booleanOptions = array(
        true  => 'Si',
        false => 'No',
    );

    public function getbooleanOptions()
    {
        return self::$booleanOptions;
    }
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('EmsPortal');
    }

    public function addEnabledPortalsQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsPortal p');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.is_enabled = ?', 1);

        return $q;
    }

    public function getEnabledPortals(Doctrine_Query $q = null)
    {
        return $this->addEnabledPortalsQuery($q)->execute();
    }

    public function addAllPortalsOrderByOrganizationQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsPortal p');
        }

        $alias = $q->getRootAlias();

        $q->addOrderBy($alias . '.organization_id');

        return $q;
    }

    public function getAllPortalsOrderByOrganization(Doctrine_Query $q = null)
    {
        return $this->addAllPortalsOrderByOrganizationQuery($q)->execute();
    }

    public function addPortalsThatCanBeAccessedByUserQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsPortal p');
        }

        $alias = $q->getRootAlias();

        $user = sfContext::getInstance()->getUser()->getGuardUser();

        $q->innerJoin($alias.'.EmsHasAccess eha WITH eha.user_id = ?', $user->getId());

        return $q;
    }

    public function getPortalsThatCanBeAccessedByUser()
    {
        return $this->addPortalsThatCanBeAccessedByUserQuery()->execute();
    }
}